package com.woniuxy.merchant.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.woniu.dto.SiteDto;
import com.woniu.dto.SiteListDto;
import com.woniu.form.AddSiteForm;
import com.woniu.form.SiteListForm;
import com.woniu.form.UpdateSiteForm;
import com.woniu.zsd.commons.core.model.Result;
import com.woniuxy.merchant.entity.Site;
import com.woniuxy.merchant.service.ISiteService;
import com.woniu.commons.util.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 站点信息表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-05-13
 */
@RestController
@RequestMapping("/site")
@Api(tags = "站点管理")
public class SiteController {
    @Resource
    private ISiteService siteService;
    @Autowired
    private TokenUtil tokenUtil;
    @ApiOperation(value = "新增站点")
    @PostMapping("/add")
    public Result addSite(@Valid @RequestBody AddSiteForm addSiteForm, @RequestHeader String token) throws Exception {
        siteService.addSite(addSiteForm);
        return Result.getResult().setCode(200).setMsg("新增站点成功");
    }
    @ApiOperation(value = "删除站点")
    @DeleteMapping("/delete")
    public Result deleteSite( Integer siteId,  @RequestHeader("token") String token) {
        Integer id = tokenUtil.getAdminId(token);
        siteService.deleteSite(siteId);
        return Result.getResult().setCode(200).setMsg("删除站点成功");
    }
    @ApiOperation(value = "修改站点")
    @PostMapping("/updateSite")
    public Result updateSite(@RequestBody UpdateSiteForm updateSiteForm, @RequestHeader("token") String token){
        Integer id = tokenUtil.getAdminId(token);
        siteService.updateSite(updateSiteForm, id);
        return Result.getResult().setCode(200).setMsg("修改站点成功");

    }
    @ApiOperation(value = "查询所有站点")
    @GetMapping("/getAllSite")
    public Result<Page<SiteListDto>> siteList(SiteListForm siteListForm) throws JsonProcessingException {
        Page<SiteListDto> siteList = siteService.siteList(siteListForm);
        return new Result<Page<SiteListDto>>().setCode(200).setMsg("查询站点成功").setData(siteList);
    }
    @ApiOperation(value = "站长查询自己的站点信息")
    @GetMapping("/getSiteByMerchantId")
    public Result<List<SiteListDto>> getSiteByMerchantId(@RequestHeader("token") String token) throws JsonProcessingException {
        Integer merchantId = tokenUtil.getAdminId(token);
        List<SiteListDto> siteList = siteService.getSiteByMerchantId(merchantId);
        return new Result<List<SiteListDto>>().setCode(200).setMsg("查询站点成功").setData(siteList);
    }
    @ApiOperation(value = "根据经纬度查询附近站点")
    @GetMapping("/recent")
    public Result<List<Site>> recentSiteId(@ApiParam(name = "longitude", value = "经度")
                                              @RequestParam(value = "longitude") Double longitude,
                                           @ApiParam(name = "latitude", value = "纬度")
                                              @RequestParam(value = "latitude") Double latitude) throws IOException {
        List<Site> siteIds = siteService.recentSiteId(longitude, latitude);
        return new Result<List<Site>>().setCode(200).setMsg("查询最近站点成功").setData(siteIds);
    }
    @ApiOperation(value = "根据站点id查询站点列表")
    @GetMapping("/siteId")
    public Result<SiteDto> siteId(Integer siteId){
        SiteDto byId = siteService.siteId(siteId);
        return new Result<SiteDto>().setCode(200).setMsg("查询站点成功").setData(byId);

    }


}
